package com.cn.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.id.IntegralDataTypeHolder;

import com.cn.domain.BigCart;
import com.cn.domain.Book;
import com.cn.domain.Cart;
import com.cn.domain.Customer;
import com.cn.domain.Order;
import com.cn.service.BookService;
import com.cn.service.CustomerService;
import com.cn.service.OrderService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class OrderAction extends ActionSupport implements ModelDriven<Order> {

	private OrderService orderService;
	private BookService bookService;
	private CustomerService customerService;
    Order model = new Order();
    //封装表单数据
    private String counts;
    private String postcode;
	@Override
	public Order getModel() {
		return model;
	}
	public CustomerService getCustomerService() {
		return customerService;
	}
   public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
   public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public String getCounts() {
		return counts;
	}
	public void setCounts(String counts) {
		this.counts = counts;
	}
	public BookService getBookService() {
		return bookService;
	}
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}
	
	
	//生成订单页面
	   //将购物车选中的书的信息保存到一个域中
	       //在订单页面用于显示

	public String getPostcode() {
		return postcode;
	}
	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}
	public String orderItems(){
		
	  HttpServletRequest request =  ServletActionContext.getRequest();
	 String[] _choice = request.getParameterValues("choice");//得到选中的id
  //接受表单改变的书的数量
	  String[] _counts   =request.getParameterValues("counts");
	
	   String []_id = request.getParameterValues("id");//隐藏域提交 得到书的Bid
		//  System.out.println("订单编号id+"+this.getId())("id");
    /* for(String string : _id){
	   System.out.println("_id======"+string);
     }*/
 if(_choice==null){
	 	  this.addActionMessage("对不起，请你选取要结算商品的信息");
	   return INPUT;
	  }
	for (int i = 0; i < _choice.length; i++) {
		System.out.println("_choice="+_choice[i]);
	}
	for (int i = 0; i < _counts.length; i++) {
		System.out.println("_counts="+_counts[i]);
	}
	  //将获取的表单参数封装成一个map集合
	  Map<Integer, Integer> newmap = new HashMap<Integer, Integer>();
	  System.out.println("_counts.size="+_counts.length);
	  for(int i=0;i<_choice.length;i++){ //
		    //分别循环两个集合
		 String beixuanchoice = _choice[i];//得到选中的id
		 int newxiabiao     = Integer.valueOf(beixuanchoice);
		 System.out.println(newxiabiao);
		 //重新封装集合
		    //书数量
		// System.out.println("quzuihouyige="+_counts[4]);
		 Integer newcounts =  Integer.valueOf( _counts[newxiabiao-1]);
		
		     //书的主键
		 Integer newbid =  Integer.valueOf(_id[newxiabiao-1]);
		
		newmap.put(newbid,newcounts);//封装数量		  
	  }
/*	  //迭代该新的集合
	for(Integer key :newmap.keySet()){
		System.out.println("orderkey="+key);
	}*/
	  //将页面改变的值重新封装到购物车中 
	      //1.首先从session中获取该购物车
	BigCart newbigCart = (BigCart)ActionContext.getContext().getSession().get("exitsbigCart");
	    //从页面获取到传值的key 及Bid
	Map<Integer, Cart> _newcartmap  =  newbigCart.getCartmap();

		//2.将改变的数量放入购物车中
	Set<Integer> setid = newmap.keySet();//得到所有书的bid  
	
	  Map<Integer, Cart> newcartmap = new HashMap<Integer,Cart>();
	   //迭代 根据setid可以查出
for (Iterator iterator = setid.iterator(); iterator.hasNext();) {
	Integer integerid = (Integer) iterator.next();
	System.out.println("integerid="+integerid);
	Book  newbook   =bookService.getBookId(integerid);//得到封装集合key  key中存放书的id
	  Cart   cart = new Cart();
    cart.setBook(newbook);
    Integer newnumbervalue = newmap.get(integerid);//得到集合value  集合value中存放的是书的个数
  //  System.out.println("newnumbervalue="+newnumbervalue);
     newbigCart.deleteCart(integerid);//删除购物车已有的集合
    cart.setNumber( newnumbervalue );
   newcartmap.put(integerid , cart); 
    //将封装好的数据重新放入购物车中
   newbigCart.setCartmap(newcartmap);
    }
   ActionContext.getContext().getSession().put("exitsbigCart", newbigCart);
   
  	return "orderpage";
	
	}
	//保存订单信息
	public String addorder(){
	Date orderDate = new Date();
	model.setOrdertime(orderDate);//下单日期
	model.setState(2);
	//得到action
	String exitcustomer =(String)ActionContext.getContext().getSession().get("exitcustomer");
	 Customer customer = customerService.getLoginUsername(exitcustomer);
	 //保存外键
	 model.setCustomer(customer);
	 //在购物车中得到商品的金额
	 BigCart bigCart = (BigCart)ActionContext.getContext().getSession().get("exitsbigCart");
	 model.setOrdertotal(bigCart.getAddTotalPrice());//订单支付多钱
	orderService.saveorder(model);
	//makeOrderAplipayUrl();
     return "apliaysuccess";
	}
	
	//根据订单生成支付宝接口的URL
	public static void makeOrderAplipayUrl() {
		HashMap hm = new HashMap();
		String patagerway="https://openapi.alipay.com/gateway.do";//支付宝接口
		/*String service ="create_direct_pay_by_user";//服务类型   快速付款交易服务
		String out_trade_no=order.getOid()+"";//订单编号  商户网站订单
		String input_charsetString="utf-8";
		String securitykey="";//支付宝校验用户的安全码(账户内提取)
		String  total="0.01";//订单总价
		String seller_emailString="";//卖家的支付宝账户
		//String notify_url=""
		//return null;
*/	
		
		
	}
//diangdanguanli
	public String findAllOrder(){
		 String _newstate =null;
		List<Order> orderlist = orderService.findAllOrder();
	/*	List<Integer> stateList = new ArrayList<Integer>();//存放zhuang
		   for(int i=0;i<orderlist.size();i++){
			  Integer newstate =  orderlist.get(i).getState();
			  System.out.println("newstate"+newstate);
		      if(newstate==2){
		    	_newstate ="订单已提交";
		      }
		  
		   if(newstate==3){
			   _newstate ="订单已发货";
		   }
		   if(newstate==3){
			   _newstate ="订单已发货";
		   }
		   */
		   //}	  
		  // ActionContext.getContext().getSession().put("state", _newstate);
		
		 
		ActionContext.getContext().getValueStack().set("orderlist", orderlist);
		return "orderindex";
	}
	/*
	 * 订单的操作
	 */
	public String editCustomerState(){
		//处理订单的状态
	Order order =	orderService.getCustomerOid(model.getOid());
	order.setState(3);//表示订单已发货
	orderService.updateCustomerState(order);
	return "orderstate";
	}
	
}
